/**
 * <p>Title: HallOfFame</p>
 * <p>Description: A representation of a hall of fame, which contains the 10 best players</p>
 * @author Guilherme Mauro Germoglio
 */

import java.io.*;
import java.util.*;

public class HallOfFame {

  // atributes
  private List highScores;
  private final String FILENAME = "hallOfFame.jas";
  private final int MAX_SIZE = 5;

  // methods

  /**
   * The HallOfFame's constructor. What else can I say?
   * @throws IOException Any of the usual Input/Output related exceptions
   * @throws ClassNotFoundException Class of a serialized object cannot be found.
   */
  public HallOfFame() throws IOException, ClassNotFoundException {
    File file = new File(FILENAME);
    if (file.exists())
      mountHallOfFame(FILENAME);
    else
      highScores = new LinkedList();
  }

  /**
   * Fills the list highScores with all data the file has
   * @param fileName The file which contains the list of high scores
   * @throws IOException Any of the usual Input/Output related exceptions
       * @throws ClassNotFoundException Class of a serialized object cannot be found.
   */
  private void mountHallOfFame(String fileName) throws IOException,
      ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
    Object data = in.readObject();
    if ( (data != null) && (data instanceof List))
      this.highScores = (List) data;
    else
      this.highScores = new LinkedList();
    in.close();
  }

  /**
   * Checks if the HighScore specified can be or not at the hall of fame
   * @param newHighScore The wanna be high score
   * @return True if the score can be in the top ten, false case not
   */
  public boolean canBeAtHallOfFame(double newHighScore) {
    int size = getSize();
    if (size >= 5) {
      if ( ( (HighScore) highScores.get(size - 1)).getScore() > newHighScore)
        return false;
    }
    return true;
  }

  /**
   * Adds a HighScore to the highScores list in its certain position
   * @param newHighScore The HighScore to be added
   */
  public void add(HighScore newHighScore) {
    Iterator i = highScores.iterator();
    int counter = 0;
    while (i.hasNext() && ( ( (HighScore) i.next()).compareTo(newHighScore) > 0)) {
      counter++;
    }
    highScores.add(counter, newHighScore);
    if (getSize() > MAX_SIZE)
      highScores.remove(getSize() - 1);
  }

  /**
   * Gets the highScores list size
   * @return The highScores list size
   */

  public int getSize() {
    return highScores.size();
  }

  /**
   * Removes an object at the specified index
   * @param index The index from where the object will be removed
   * @return The removed object
   */

  public Object remove(int index) {
    return highScores.remove(index);
  }

  /**
   * Records all data in disk
   * @throws IOException Any of the usual Input/Output related exceptions
       * @throws ClassNotFoundException Class of a serialized object cannot be found.
   */

  public void recordInDisk() throws IOException, ClassNotFoundException {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
        FILENAME));
    out.writeObject(highScores);
    out.close();
  }

  /**
   * A String representation of the hall of fame
   * @return A String representation
   */

  public String toString() {
    String theReturn = "";
    String lineSeparator = System.getProperty("line.separator");
    Iterator i = highScores.iterator();
    while (i.hasNext()) {
      String highScore = i.next().toString();
      theReturn += "    " + highScore + lineSeparator;
    }
    if (theReturn.equals(""))
      return "No high scores.";
    return theReturn;
  }
}